package com.htandroid.htandroid.aty;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

import com.ab.soap.AbSoapListener;
import com.ab.soap.AbSoapParams;
import com.ab.task.AbTask;
import com.ab.task.AbTaskItem;
import com.ab.task.AbTaskObjectListener;
import com.ab.util.AbDialogUtil;
import com.ab.util.AbToastUtil;
import com.ab.view.ioc.AbIocView;
import com.htandroid.htandroid.R;
import com.htandroid.htandroid.entity.Vendor;
import com.htandroid.htandroid.sys.InstorageURL;
import com.htandroid.htandroid.sys.User;
import com.htandroid.htandroid.sys.base_index;
import com.htandroid.htandroid.webservice.abXMLVendorlist;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import static com.htandroid.htandroid.constance.ConstanceServer.BaseUrl_New;

/**
 * Created by lkp on 2015/11/29.
 */
//存货档案选择
public class Basenvo_Choosecven extends base_index {
    private ArrayList<Vendor> venlist;
    private String btntype;
    private String ccode;

    @AbIocView(id = R.id.btn_tv, click = "btnClick")
    TextView btn_tv;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setAbContentView(R.layout.basenvo_rchoose);
        initview("供应商档案");
        initview2();
    }

    public void btnClick(View v) {
        Intent intent = new Intent();
        //从position改为id,因为过滤之后的position不正确
        intent.putExtra("cvencode", "");
        intent.putExtra("btntype", btntype);
        intent.putExtra("cvenname", "");
        intent.putExtra("cvenemail", "");
        setResult(R.layout.basenvo_rchoose / 200000, intent);  //设置返回结果
        finish();
    }

    //初始化2
    @Override
    public void initview2() {
        //获取传递的信息
        Intent intent = getIntent();// 创建Intent对象
        Bundle bundle = intent.getExtras();// 获取传入的数据，并使用Bundle记录
        btntype = bundle.getString("btntype");
        ccode = bundle.getString("ccode");
        venlist = myApplication.getGv_vendor();
        if (venlist == null || venlist.size() < 1) {
            //获取供应商列表
            getdata(0);
        } else {
            createrl();
        }

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Vendor cven = venlist.get((int) id);
                Intent intent = new Intent();
                //从position改为id,因为过滤之后的position不正确
                intent.putExtra("cvencode", cven.getCvenCode());
                intent.putExtra("btntype", btntype);
                intent.putExtra("cvenname", cven.getCvenName());
                intent.putExtra("cvenemail", cven.getCvenEmail());
                setResult(R.layout.basenvo_rchoose / 200000, intent);  //设置返回结果
                finish();
            }
        });
    }

    //获取数据
    @Override
    public void getdata(int page) {
//        TreeMap<String, String> properties = new TreeMap<String, String>();
//        //参数
//        String[] parms = {"usercode", "cacc_id", "iyear", "ddate"};
//        String[] vals = {User.uid, myApplication.getGs_ztid(), myApplication.getGs_year(), myApplication.getGs_sj()};
//        //生成xml
//        BuildQgdListXMLService service = new BuildQgdListXMLService();
//        String xmlstring = service.buildXML(null, "vendorlist", InstorageURL.NAME_SPACE, parms, vals, null);
//
//        ToolSOAPxml.callService("vendorlist", xmlstring, new ToolSOAPxml.WebServiceCallBack() {
//            @Override
//            public void onSucced(String result) {
//                //解析xml文件,返回列表
//                if (result != null) {
//                    venlist = XMLVendorlist.paraseCommentInfors(result);
//                    myApplication.setGv_vendor(venlist);
//                    createrl();
//                } else {
//                    AbToastUtil.showToastInThread(getApplicationContext(), R.string.sqlerrormsg);
//                }
//            }
//
//            @Override
//            public void onFailure(String result) {
//                AbToastUtil.showToastInThread(getApplicationContext(), R.string.sqlerrormsg);
//                loadList();
//                //swipeRefreshLayout.setRefreshing(false);
//                //ToolLog.log("err：" + result);
//            }
//        });

        AbTask task = new AbTask();
        final AbTaskItem item = new AbTaskItem();
        item.setListener(new AbTaskObjectListener() {

            @Override
            public <T> void update(T entity) {
                String urlString = "http://" + InstorageURL.SERVERURL + BaseUrl_New;//旧版webservice地址
                AbSoapParams params = new AbSoapParams();
                params.put("arg0", User.uid);
                params.put("arg1", myApplication.getGs_ztid());
                params.put("arg2", myApplication.getGs_year());
                params.put("arg3", myApplication.getGs_sj());
                adatperDataList.clear();
                mAbSoapUtil.call(urlString, InstorageURL.NAME_SPACE, "vendorlist", params, new AbSoapListener() {
                    //获取数据成功会调用这里
                    @Override
                    public void onSuccess(int statusCode, String content) {
                        if (content != null) {
                            venlist = abXMLVendorlist.paraseCommentInfors(content);
                            myApplication.setGv_vendor(venlist);
                            createrl();
                        } else {
                            AbToastUtil.showToast(Basenvo_Choosecven.this, R.string.sqlerrormsg);
                        }
                    }

                    // 失败，调用
                    @Override
                    public void onFailure(int statusCode, String content,
                                          Throwable error) {
                        AbToastUtil.showToast(getApplicationContext(), R.string.sqlerrormsg);
                        loadList();
                    }

                    // 开始执行前
                    @Override
                    public void onStart() {
                        //显示进度框
                        AbDialogUtil.showProgressDialog(Basenvo_Choosecven.this, 0, "正在加载...");
                    }

                    // 完成后调用，失败，成功
                    @Override
                    public void onFinish() {
                        //移除进度框
                        AbDialogUtil.removeDialog(Basenvo_Choosecven.this);
                    }
                });
            }

            @SuppressWarnings("unchecked")
            @Override
            public Integer getObject() {
                return -1;
            }
        });
        task.execute(item);
    }

    public void createrl() {
        adatperDataList.clear();
        ArrayList<Map<String, Object>> dataListForInit = new ArrayList<Map<String, Object>>();
        for (int i = 0; i < venlist.size(); i++) {
            Vendor cven = new Vendor();
            cven = venlist.get(i);
            //增加一个行选择状态初始化
            if (ccode != null && cven.getCvenCode().equals(ccode)) {
                sa.getIsSelected().put(i, true);
            } else {
                sa.getIsSelected().put(i, false);
            }
            //==========================
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("index", (i + 1));
            map.put("cvouchid", "名称：" + cven.getCvenName());
            map.put("cdept", "地址：" + cven.getCvenAddress());
            map.put("cnocheckreason", "联系电话：" + cven.getCvenPhone());
            map.put("ddate", "电子邮件：" + cven.getCvenEmail());
            map.put("data5", "编号：" + cven.getCvenCode());
            map.put("data6", "简称：" + cven.getCvenAbbName());
            map.put("data7", "");
            map.put("data8", "");
            dataListForInit.add(map);
        }
        toolDataProvider.initData(dataListForInit);
        loadList();
        //loadingDialog.loadingfinish();
        //mHandler.sendEmptyMessage(LIST_GET_FIRST_PAGE); //加载完毕发送指令显示第一页
        //swipeRefreshLayout.setRefreshing(false);
    }
}
